package com.esri.core.internal.io.handler;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.message.BasicHeader;

/* loaded from: classes.dex */
public class i implements HttpEntity {
    static final byte[] d = "---------------------------".getBytes();
    File a;
    String b;
    String c;
    final byte[] e;
    final byte[] f;

    public Header getContentEncoding() {
        return null;
    }

    public boolean isRepeatable() {
        return true;
    }

    public i(InputStream inputStream, String str, String str2, String str3) throws IOException {
        this(inputStream, str, str2, str3, null);
    }

    public i(InputStream inputStream, String str, String str2, String str3, Map<String, Object> map) throws IOException {
        this.b = str3;
        this.e = Long.toString(System.currentTimeMillis()).getBytes();
        this.c = str2;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[1024];
        while (true) {
            int read = inputStream.read(bArr);
            if (read == -1) {
                break;
            }
            byteArrayOutputStream.write(bArr, 0, read);
        }
        byteArrayOutputStream.close();
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
        if (map != null) {
            for (String str4 : map.keySet()) {
                a(byteArrayOutputStream2, map.get(str4), str4);
            }
        }
        a(byteArrayOutputStream2, str, byteArray);
        byteArrayOutputStream2.close();
        this.f = byteArrayOutputStream2.toByteArray();
    }

    public i(File file, String str, String str2) throws IOException {
        this(file, str, str2, (Map<String, Object>) null);
    }

    public i(File file, String str, String str2, Map<String, Object> map) throws IOException {
        this.a = file;
        this.b = str2;
        this.e = Long.toString(System.currentTimeMillis()).getBytes();
        this.c = str;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        if (map != null) {
            for (String str3 : map.keySet()) {
                a(byteArrayOutputStream, map.get(str3), str3);
            }
        }
        a(byteArrayOutputStream, file.getName(), a(file));
        byteArrayOutputStream.close();
        this.f = byteArrayOutputStream.toByteArray();
    }

    void a(ByteArrayOutputStream byteArrayOutputStream, Object obj, String str) throws IOException {
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.write(d);
        byteArrayOutputStream.write(this.e);
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
        byteArrayOutputStream.write(("Content-Disposition: form-data; name=\"" + str + "\"\n").getBytes());
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
        byteArrayOutputStream.write(obj.toString().getBytes());
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
    }

    void a(ByteArrayOutputStream byteArrayOutputStream, String str, byte[] bArr) throws IOException {
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.write(45);
        byte[] bArr2 = d;
        byteArrayOutputStream.write(bArr2);
        byteArrayOutputStream.write(this.e);
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
        byteArrayOutputStream.write(("Content-Disposition: form-data; name=\"" + this.c + "\"; filename=\"").getBytes());
        byteArrayOutputStream.write(str.getBytes());
        byteArrayOutputStream.write("\"\r\n".getBytes());
        byteArrayOutputStream.write("Content-Type: ".getBytes());
        String str2 = this.b;
        if (str2 == null) {
            str2 = "Content-Type: application/octet-stream";
        }
        byteArrayOutputStream.write(str2.getBytes());
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
        byteArrayOutputStream.write(bArr);
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.write(bArr2);
        byteArrayOutputStream.write(this.e);
        byteArrayOutputStream.write(13);
        byteArrayOutputStream.write(10);
        byteArrayOutputStream.write("Content-Disposition: form-data; name=\"f\"\r\n".getBytes());
        byteArrayOutputStream.write("\r\njson\r\n".getBytes());
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.write(bArr2);
        byteArrayOutputStream.write(this.e);
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.write(45);
        byteArrayOutputStream.flush();
    }

    private static final byte[] a(File file) throws IOException {
        FileInputStream fileInputStream;
        FileInputStream fileInputStream2 = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (Throwable th) {
            th = th;
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bArr = new byte[1024];
            while (true) {
                int read = fileInputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream.write(bArr, 0, read);
                } else {
                    byteArrayOutputStream.close();
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    fileInputStream.close();
                    return byteArray;
                }
            }
        } catch (Throwable th2) {
            th = th2;
            fileInputStream2 = fileInputStream;
            if (fileInputStream2 != null) {
                fileInputStream2.close();
            }
            throw th;
        }
    }

    public boolean isChunked() {
        return !isRepeatable();
    }

    public long getContentLength() {
        byte[] bArr = this.f;
        if (bArr == null) {
            return 0L;
        }
        return bArr.length;
    }

    public Header getContentType() {
        return new BasicHeader("Content-Type", "multipart/form-data; boundary=" + new String(d) + new String(this.e));
    }

    public InputStream getContent() throws IllegalStateException {
        throw new UnsupportedOperationException("Multipart form entity does not implement #getContent()");
    }

    public void writeTo(OutputStream outputStream) throws IOException {
        outputStream.write(this.f);
        outputStream.flush();
        outputStream.close();
    }

    public boolean isStreaming() {
        return !isRepeatable();
    }

    public void consumeContent() {
        if (isStreaming()) {
            throw new UnsupportedOperationException("Streaming entity does not implement #consumeContent()");
        }
    }
}
